/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.session;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Tuple;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import jpa.entities.ItemPedido;

/**
 *
 * @author carlos
 */
@Stateless
public class ItemPedidoFacade extends AbstractFacade<ItemPedido> {

    @PersistenceContext(unitName = "LaCruz_v2PU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ItemPedidoFacade() {
        super(ItemPedido.class);
    }

    public List<ItemPedido> findAllPendentes(int[] range) {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<ItemPedido> query = builder.createQuery(ItemPedido.class);
        Root<ItemPedido> ItemPedido = query.from(ItemPedido.class);

        List<Predicate> predicateList = new ArrayList<Predicate>();
        predicateList.add(builder.equal(ItemPedido.get("statusPedido"), "P"));

        Predicate[] predicates = new Predicate[predicateList.size()];
        query.where(predicateList.toArray(predicates));
        query.select(ItemPedido);

        javax.persistence.Query q = getEntityManager().createQuery(query);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();

    }
}
